package com.sam.im.heiqun.uis.activities;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.ImageView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.MessageEntivity;
import com.sam.im.heiqun.uis.adapters.AccountAdapter;
import com.sam.im.heiqun.uis.widgets.pulltorefresh.WrapContentLinearLayoutManager;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.view.PullToRefreshListener;
import com.sam.im.heiqun.view.PullToRefreshRecyclerView;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 款项到账详情界面
 */
public class AccountActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.recyclerview)
    PullToRefreshRecyclerView recyclerview;
    private List<ImMessage> datas = new ArrayList<>();
    int page = 0 ;
    int number = 15 ;
    private AccountAdapter accountAdapter   ;
    @Override
    public int getContentViewId() {
        return R.layout.activity_account;
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.samim_pay);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
//        EventBus.getDefault().register(this);
        getData();
        accountAdapter = new AccountAdapter(this,datas);
        recyclerview.setAdapter(accountAdapter);
        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerview.setLoadingMoreEnabled(false);
        recyclerview.setPullToRefreshListener(new PullToRefreshListener() {
            @Override
            public void onRefresh() {
                page++;
                getData();
            }
            @Override
            public void onLoadMore() {
            }
        });
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //  int id = (int) destid;
        notificationManager.cancel(0);//注销系统通知 系统通知id为0
        clearMessage();
    }

    @Override
    protected void onDestroy() {
//        EventBus.getDefault().unregister(this);
        datas.clear();
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //
    public void onsendvedio(String s) {
        if (s.equals("刷新")) {
          accountAdapter.notifyDataSetChanged();
          recyclerview.smoothScrollToPosition(accountAdapter.getItemCount());
          recyclerview.setRefreshComplete();
        }
    }
    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<ImMessage> imMessage  = ImMessage.find(ImMessage.class,"uniqueness==? and message_type in(?,?,?,?)",
                        new String[]{ToolsUtils.getMyUserId()+"___",
                                Constant.MESSAGE_TYPR_TRANSFOR_ISCOME +"",
                                Constant.MESSAGE_TYPR_TOPAYFOR+"",
                                Constant.NOTICE_OF_MONEY_ADD_FOR_JOIN_GROUP+"",
                                Constant.NOTICE_OF_MONEY_REDUCE_JOIN_GROUP+""}, null, "send_time asc", page * number + "," + number);

                Log.i("info","img==="+imMessage.toString()+"\n"+ToolsUtils.getMyUserId());
                if (imMessage.size() != 0) {
                    for (int i = 0; i < imMessage.size(); i++) {
                        if (!imMessage.get(i).getUniqueness().equals(ToolsUtils.getMyUserId()+"___")){
                            imMessage.remove(i);
                        }
                    }
                    if (imMessage.size()<15){
                        recyclerview.setPullRefreshEnabled(false);
                    }
                    //imMessage.addAll(datas);
                    //  Log.i("info","size=="+size+" tarposition=="+tarPosition+" positon=="+position+" page=="+page);
                    //datas.clear();
                    datas.addAll(imMessage);
                    EventBus.getDefault().post("刷新");
                 }else {
                    EventBus.getDefault().post("刷新");
                    recyclerview.setPullRefreshEnabled(false);
                }
            }
        }).start();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    private void clearMessage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //清空当前聊天对象的未读信息
                List<MessageEntivity> messageEntivities = MessageEntivity.find(MessageEntivity.class, "type=? or type=?", new String[]{Constant.MESSAGE_TYPR_TRANSFOR_ISCOME+"",Constant.MESSAGE_TYPR_TOPAYFOR+""});
                if (messageEntivities.size() > 0) {
                    MessageEntivity messageEntivity = messageEntivities.get(0);
                    messageEntivity.setMessageNum(0);
                    MessageEntivity.save(messageEntivity);
                }
                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);
            }
        }).start();

    }

    @OnClick(R.id.pre_v_back)
    public void onViewClicked() {
        finish();
    }
}
